package com.sgb.kjwl.view.ui.adapter.pick;

import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.TenderListEntity;
import com.sgb.kjwl.view.ui.activity.pick.TenderDetailsActivity3;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.ResolutionUtil;

import java.util.List;

public class PickMapAdapter extends BaseRecycleAdapter<TenderListEntity> {

    private CallBack callBack;
    private int width;
    private Context context;
    private TextView tvUserFocus;
    private ImageView imageUserFocus;

    public PickMapAdapter(Context context, List<TenderListEntity> datas, CallBack callBack) {
        super(context, R.layout.item_map_pick, datas);
        width = ResolutionUtil.getScreenWidth(context)  / 20 * 19;
        this.callBack = callBack;
        this.context = context;
    }

    @Override
    public void convert(ItemViewHolder holder, final TenderListEntity entity) {
        holder.setBinding(BR.viewModel, entity);

        if (holder.itemView.getLayoutParams().width != width) {
            holder.itemView.getLayoutParams().width = width;
        }

        TextView name = holder.itemView.findViewById(R.id.name);
        TextView tvType = holder.itemView.findViewById(R.id.tv_type);
        TextView tvTypeVertical = holder.itemView.findViewById(R.id.tv_type_vertical);
        TextView tvState = holder.itemView.findViewById(R.id.tv_state);
        TextView tvSpace = holder.itemView.findViewById(R.id.tv_space);
        TextView tvTimeTitle = holder.itemView.findViewById(R.id.tv_time_title);
        TextView tvTimeContext = holder.itemView.findViewById(R.id.tv_time_context);
        TextView tvMoneyTitle = holder.itemView.findViewById(R.id.tv_money_title);
        TextView tvMoneyContext = holder.itemView.findViewById(R.id.tv_money_context);
        TextView tvCompanyTitle = holder.itemView.findViewById(R.id.tv_company_title);
        TextView tvCompanyContext = holder.itemView.findViewById(R.id.tv_company_context);
        TextView tvAddressContext = holder.itemView.findViewById(R.id.tv_address_context);
        TextView tvCommit = holder.itemView.findViewById(R.id.tv_commit);

        tvType.setVisibility(View.GONE);
        name.setText(entity.getName());
        tvTypeVertical.setText(entity.getTypeName());
        tvSpace.setText("距您" + entity.getDistance());
        tvMoneyTitle.setText("概算金额：");
        tvMoneyContext.setText(entity.getPrice());
        tvCompanyTitle.setText("招标企业：");
        tvCompanyContext.setText(entity.getCompanyName());
        tvAddressContext.setText(entity.getAddress());

        switch (entity.getStatus()) {
            case "2":
                tvTimeTitle.setText("投标结束时间：");
                tvTimeContext.setText(entity.getEndTime1());
                tvState.setText("正在招标");
                updateViewData(tvCommit, "我要投标", 0);
                break;

            case "6":
                tvTimeTitle.setText("投标结束时间：");
                tvTimeContext.setText(entity.getEndTime1());
                tvState.setText("再次招标");
                updateViewData(tvCommit, "我要投标", 0);
                break;
            case "4":
                tvTimeTitle.setText("定标时间：");
                tvTimeContext.setText(entity.getEndTime1());
                tvState.setText("招标定标");
                updateViewData(tvCommit, "我要投标", 1);
                break;
            case "9":
                tvTimeTitle.setText("报名结束时间：");
                tvTimeContext.setText(entity.getApplyEndTime1());
                tvState.setText("招标报名");
                updateViewData(tvCommit, "我要报名", 0);
                break;
            case "3":
                tvTimeTitle.setText("开标时间：");
                tvTimeContext.setText(entity.getEndTime1());
                tvState.setText("竞标评标");
                updateViewData(tvCommit, "我要投标", 1);
                break;
            case "1":
                tvTimeTitle.setText("投标开始时间：");
                tvTimeContext.setText(entity.getStartTime1());
                tvState.setText("即将招标");
                updateViewData(tvCommit, "我要投标", 1);
                break;
            case "8":
                tvTimeTitle.setText("报名开始时间：");
                tvTimeContext.setText(entity.getStartTime1());
                tvState.setText("招标预告");
                updateViewData(tvCommit, "我要报名", 1);
                break;
        }

        holder.itemView.findViewById(R.id.img_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.onDismiss();
            }
        });

        imageUserFocus = holder.itemView.findViewById(R.id.image_user_focus);
        tvUserFocus = holder.itemView.findViewById(R.id.tv_user_focus);
        updateFocus(entity.getUserFocusFlag());

        holder.itemView.findViewById(R.id.tv_report).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                callBack.onReport(entity);
            }
        });
        holder.itemView.findViewById(R.id.tv_contact_service).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                callBack.onContactService(entity);
            }
        });
        holder.itemView.findViewById(R.id.tv_inquire_by_letter).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                callBack.onInquireByLetter(entity);
            }
        });
        holder.itemView.findViewById(R.id.line_user_focus).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                callBack.onLineUserFocus(entity.getId(), holder.getLayoutPosition());
            }
        });
        tvCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TenderDetailsActivity3.start(context, entity.getId(), 0);
            }
        });
    }

    public void updateFocus(int userFocusFlag) {
        if (userFocusFlag == 0) {
            imageUserFocus.setBackgroundResource(R.mipmap.guanzhu_pick);
            tvUserFocus.setText("关注");
            tvUserFocus.setTextColor(context.getResources().getColor(R.color.color_tab_one));
        } else if (userFocusFlag == 1) {
            imageUserFocus.setBackgroundResource(R.mipmap.yiguanzhu_pick);
            tvUserFocus.setText("已关注");
            tvUserFocus.setTextColor(context.getResources().getColor(R.color.color_main));
        }
    }

    public void updateFocusForPosition(int position) {
        int userFocusFlag1 = datas.get(position).getUserFocusFlag();
        datas.get(position).setUserFocusFlag(userFocusFlag1 == 0 ? 1 : 0);
        notifyItemChanged(position);
    }

    private void updateViewData(TextView tvCommit, String tvCommitText, int tvCommitBg) {
        if (!TextUtils.isEmpty(tvCommitText)) {
            tvCommit.setText(tvCommitText);
        } else {
            tvCommit.setText("");
        }
        if (tvCommitBg == 0) {
            tvCommit.setBackground(ContextCompat.getDrawable(context, R.drawable.tender_details));
            tvCommit.setEnabled(true);
        } else {
            //if(tvCommitBg == 1)
            tvCommit.setBackground(ContextCompat.getDrawable(context, R.drawable.tender_dateils_unclick));
            tvCommit.setEnabled(false);
        }
    }

    public interface CallBack {
        void onDismiss();

        void onReport(TenderListEntity tenderListEntity);

        void onContactService(TenderListEntity tenderListEntity);

        void onInquireByLetter(TenderListEntity tenderListEntity);

        void onLineUserFocus(String tenderNo, int position);
    }
}
